/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.barnett.tool;

import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DbOpenHelper extends SQLiteOpenHelper {
	/** 数据库版本 */
	private static final int DATABASE_VERSION = 1;
	private static DbOpenHelper instance;
	/** 数据库名称 */
	private final static String DATABASE_NAME = "database.db";

	private DbOpenHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	public static DbOpenHelper getInstance(Context context) {
		if (instance == null) {
			instance = new DbOpenHelper(context.getApplicationContext());
		}
		return instance;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
	}

	/**
	 * 新建一个表
	 * 
	 * @param 提示
	 *            INTEGER PRIMARY KEY AUTOINCREMENT 自增长主键 TEXT 文本
	 * @param db
	 */
	public void CreateTable(Context context, String tableName, String value) {
		SQLiteDatabase db = getInstance(context).getWritableDatabase();
		// 如果这个表不存在创建一个表
		String sql = "create table if not exists " + tableName + "(" + value
				+ ")";
		try {
			db.execSQL(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 往表里插入数据
	 * 
	 * @param context
	 * @param table
	 * @param nullColumnHack
	 *            当values为空 则指定一列参数为null
	 * @param values
	 */
	public void Insert(Context context, String table, String nullColumnHack,
			ContentValues values) {
		SQLiteDatabase db = getInstance(context).getWritableDatabase();
		db.insert(table, nullColumnHack, values);
	}

	/**
	 * 更新表的数据
	 * 
	 * @param context
	 * @param table
	 * @param values
	 * @param whereClause
	 *            要更新的数据的条件
	 * @param whereArgs
	 *            条件的参数
	 */
	public void Update(Context context, String table, ContentValues values,
			String whereClause, String[] whereArgs) {
		// TODO Auto-generated method stub
		SQLiteDatabase db = getInstance(context).getWritableDatabase();
		db.update(table, values, whereClause, whereArgs);
	}

	/**
	 * 删除数据
	 * 
	 * @param context
	 * @param table
	 * @param whereClause
	 * @param whereArgs
	 */
	public void Delete(Context context, String table, String whereClause,
			String[] whereArgs) {
		// TODO Auto-generated method stub
		SQLiteDatabase db = getInstance(context).getWritableDatabase();
		db.delete(table, whereClause, whereArgs);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

	/**
	 * 关闭数据库
	 */
	public void closeDB() {
		if (instance != null) {
			try {
				SQLiteDatabase db = instance.getWritableDatabase();
				db.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			instance = null;
		}
	}

}
